
package us.hornerscorners.lollipop.hapi;

/*
* #%L
 * * LollipopRouter
 * *
 * %%
 * Copyright (C) 2013 - 2014 Jim Horner
 * *
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import java.util.List;

import java.util.concurrent.TimeUnit;

import ca.uhn.hl7v2.HapiContext;

import ca.uhn.hl7v2.app.Connection;
import ca.uhn.hl7v2.app.Initiator;

import ca.uhn.hl7v2.model.Message;

import ca.uhn.hl7v2.protocol.ReceivingApplicationException;

import com.google.common.base.Splitter;

import com.google.common.primitives.Ints;

import us.hornerscorners.lollipop.config.RoutingConfig;


/**
 * Class description
 *
 *
 * @version        v1.0, 2014-09-08
 * @author         Jim Horner
 */
public class LlpClient implements IClient {

    /** Field description */
    private final HapiContext hapiContext;

    /**
     * Constructs ...
     *
     *
     * @param hapiContext
     */
    public LlpClient(HapiContext hapiContext) {

        super();
        this.hapiContext = hapiContext;
    }

    /**
     * Method description
     *
     *
     * @param endpoint
     *
     * @return
     */
    @Override
    public boolean canHandle(String endpoint) {

        return endpoint.startsWith("llp://");
    }

    /**
     * Method description
     *
     *
     *
     * @param config
     * @param message
     *
     * @return
     *
     * @throws ReceivingApplicationException
     */
    @Override
    public Message sendMessage(RoutingConfig config, Message message)
            throws ReceivingApplicationException {

        String place = config.getEndpoint().replace("llp://", "");
        List<String> pieces = Splitter.on(":").splitToList(place);

        if (pieces.size() != 2) {

            throw new ReceivingApplicationException(
                "LLP Endpoint is missing hostname and/or port.");
        }

        Integer port = Ints.tryParse(pieces.get(1));

        if (port == null) {

            throw new ReceivingApplicationException(
                "LLP Endpoint is missing port and/or port is invalid.");
        }

        Message result = null;
        Connection conn = null;

        try {

            conn = this.hapiContext.newClient(pieces.get(0), port, false);

            Initiator initiator = conn.getInitiator();
            Integer responseTimeout = config.getResponseTimeout();

            if (responseTimeout != null) {

                initiator.setTimeout(responseTimeout, TimeUnit.MILLISECONDS);
            }

            result = initiator.sendAndReceive(message);

        } catch (Exception e) {

            throw new ReceivingApplicationException(e);

        } finally {

            if (conn != null) {

                conn.close();
            }
        }

        return result;
    }
}
